package com.terrynoya.fight.stateMachine.triggers
{
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.char.Entity;
	import com.terrynoya.fight.combat.EntityCollection;
	import com.terrynoya.fight.combat.HitDefinition;
	import com.terrynoya.fight.evaluation.Node;
	import com.terrynoya.fight.evaluation.ParseState;

	public class InGuardDist
	{
		public function InGuardDist()
		{
			
		}
		
		public static function eval(data:Object,node:Node):*
		{
			var chara:Character = Character(data);
			if(chara == null)
			{
				return false;
			}
			var entities:EntityCollection = chara.engine.entities;
			for (var i:int = 0; i < entities.length; i++) 
			{
				var entity:Entity = entities.getChildAt(i);
				var opp:Character = chara.filterEntityAsCharacter(entity);
				if(opp != null && opp.offensiveInfo.activeHitDef == true 
					&& inGuardDistance(opp,chara,opp.offensiveInfo.hitDef))
				{
					return true;
				}
			}
			return false;
		}
		
		public static function inGuardDistance(attacker:Entity,target:Character,hitDef:HitDefinition):Boolean
		{
			var distance:Number = Math.abs(attacker.position.x - target.position.x);
			return distance <= hitDef.guardDistance;
		}
		
		public static function parse(state:ParseState):Node
		{
			return state.baseNode;
		}
	}
}